package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealDishMapeer;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    /*
     *   返回套餐关系SetmealDto
     * */
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapeer setmealDishMapeer;
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    public R addWithDish(SetmealDto setmealDto) {
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //先判断是否选择菜品
        if (setmealDishes==null){
            return R.error("请添加菜品套餐");
        }
        //保存数据到套餐表中
        this.save(setmealDto);
        //获取套餐id
        Long setmealDtoId = setmealDto.getId();
        //获取套餐数据集合
        //遍历套餐集合  给每个套餐设置id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDtoId);
            int insert = setmealDishMapeer.insert(setmealDish);
        }
        return R.success("添加成功");
    }

    /*
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     *
     */
    @Transactional
    public R removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lq = new LambdaQueryWrapper<>();
        lq.in(Setmeal::getId,ids);
        lq.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(lq);
        if (count>0){
            //不能删除，抛出一个业务异常
            throw new CustomException("套餐售卖中，不能删除");
        }
        //么有关联可以删除
        setmealMapper.deleteBatchIds(ids);
        //删除关联套餐关系表中的数据
        LambdaQueryWrapper<SetmealDish> setmealDishLwq = new LambdaQueryWrapper<>();
        setmealDishLwq.in(SetmealDish::getSetmealId,ids);
        setmealDishMapeer.delete(setmealDishLwq);
        return R.success("删除成功");
    }


    /*
    *   修改套餐
    * */
    @Override
    public R<SetmealDto> updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        //通过id获取具体套餐
        Setmeal setmeal = this.getById(setmealDto.getId());
        //将获取到的具体套餐拷贝到 setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //获取套餐id
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        //调用业务层根据套餐id删除菜品
        setmealDishMapeer.delete(lqw);
        //获取套餐菜品的集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //遍历集合 给每个菜品注入套餐id
            setmealDish.setSetmealId(setmealId);
            //调用业务层完成菜品修改
            setmealDishMapeer.insert(setmealDish);
        }
        return R.success(setmealDto);
    }
}
